package com.shouhuyun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.entity.Result;
import com.shouhuyun.service.ReMemberService;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/re")
public class reController {
    @Reference
    private ReMemberService reMemberService;


    //会员男女占比饼状图
    @RequestMapping("/getReport")
    public Result getReport() {
        Map<String, Object> data = new HashMap<>();

        try {
            List<Map<String, Object>> reportCount = reMemberService.findReportCount();  // 根据性别分组 查 每种性别的 个数
            data.put("reportCount", reportCount);

            List<String> reportsSex = new ArrayList<>();
            for (Map<String, Object> map : reportCount) {
                Integer sex = Integer.parseInt((String) map.get("sex"));
                if (sex == 1) {
                    map.put("name", "男");
                } else {
                    map.put("name", "女");
                }
                String ss = (String) map.get("name");
                reportsSex.add(ss);
            }
            data.put("reportSex", reportsSex);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    //会员年龄比列饼状图
    @RequestMapping("/getReportAag")
    public Result getReportAge(){
        // 返回给前端页面的data
        Map<String, Object> data = new HashMap<>();
        try {
            List<Map<String, Object>> reportCount = reMemberService.findReportCountById(); // 查出四个年龄段数据（ 年龄段 和 年龄段的人数 ）

            data.put("reCount",reportCount);
            List<String> reName = new ArrayList<>();

            for (Map<String, Object> map : reportCount) {
                String name = (String) map.get("name");
                reName.add(name);
            }
            data.put("reName",reName);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL,data);
        }
    }
}
